<?php
/**
 * @class pages Glossary module
 * @author Bethrezen <admin@devgroup.ru>
 * @desc Handles all Glossary related stuff.
 */
class glossary
{
	var $core;
	
	var $tableName = 't_glossary';
	
	var $action='';
	
	function glossary(&$_core)
	{
		
		
		$this->core = $_core;
		
		$this->core->adminLinks['glossary'] = 'Словарь';
		
		if (!file_exists(DB_DIR . $this->core->config->dbName . "/" . $this->tableName . ".txt"))
		{
		
			$SQL = "CREATE TABLE " . $this->tableName . " ( id inc , word str , content str )";
			
			$this->core->db->executeQuery($SQL);
			
			$this->core->db->executeQuery("INSERT INTO " . $this->tableName . " ( word , content ) VALUES ( 'FCore' , 'FCore - небольшой фреймворк' )");
			$this->core->db->executeQuery("INSERT INTO " . $this->tableName . " ( word , content ) VALUES ( 'FConfig' , 'Класс конфигурации FCore' )");
			$this->core->db->executeQuery("INSERT INTO " . $this->tableName . " ( word , content ) VALUES ( 'HTML' , 'Hyper text mark up language' )");
			$this->core->db->executeQuery("INSERT INTO " . $this->tableName . " ( word , content ) VALUES ( 'Азбука' , 'Книжка такая большая и с картинками' )");
		}
		
		$action = 'main';
		if (isset($_GET['actionGlossary']))
			$action = $_GET['actionGlossary'];
		if (isset($_POST['actionGlossary']))
			$action = $_POST['actionGlossary'];
				
		$this->action = $action;
		
	}
		
	/**
	 * Returns the FAQ page.
	 *
	 */
	function getGlossary()
	{
		$records = array();
		$q4glossary = $this->core->db->executeQuery("SELECT * FROM " . $this->tableName . " ORDER BY word ASC");
		if ($q4glossary->getRowCount() != 0)
		{
			while ($q4glossary->next())
			{
				$record = $q4glossary->getCurrentValuesAsHash();
				if (!empty($record['word']))
					$record['firstLetter'] = strtoupper($this->core->utf8_substr($record['word'],0,1));
				else
					$record['firstLetter'] = '[void]';
				$records[]=$record;
			}
			
			
		}
		
		return ($records);
	}
	
	/**
	 * Mian admin action
	 *
	 */
	function admin()
	{
		
		$actFunc = "admin" . $this->action;
		$this->$actFunc();
	}
	
	function adminMain()
	{	
		
		$q4glossary = $this->core->db->executeQuery("SELECT * FROM " . $this->tableName . " ORDER BY word ASC  ");
		if ($q4glossary->getRowCount() != 0)
		{
			?>
			<h2>Словарь</h2>
			<table cellpadding="0" cellspacing="2" border="0"><TR><td>Слово</td><td>Действия</td></TR>
			
			<?php
		
			while ($q4glossary->next())
			{
				$record = $q4glossary->getCurrentValuesAsHash();
				?>
				<tr><TD><?=$record['word'];?></TD><TD><a href="<?=basename($_SERVER['SCRIPT_FILENAME']);?>?actAdmin=glossary&actionGlossary=Edit&id=<?=$record['id']?>">Редактировать</a> <a href="<?=basename($_SERVER['SCRIPT_FILENAME']);?>?actAdmin=glossary&actionGlossary=Remove&id=<?=$record['id']?>">Удалить</a></TD></tr>
				<?php
			}	
				
			?>
			</table>
			<a href="<?=basename($_SERVER['SCRIPT_FILENAME']);?>?actAdmin=glossary&actionGlossary=Add">Добавить</a>
			<?php
		}
		
	}
	
	function adminRemove()
	{
		if (isset($_GET['id']))
		{
			$SQL = "DELETE FROM " . $this->tableName . " WHERE  id   = '" . intval($_GET['id']) . "' ";
			
				$this->core->db->executeQuery($SQL);
		}
		
		$this->adminMain();
	}
	
	function adminEdit()
	{
		if (isset($_GET['id']))
		{
			$q4edit = $this->core->db->executeQuery("SELECT * FROM " . $this->tableName . " WHERE id = '" . intval($_GET['id']) . "'");
			if ($q4edit->getRowCount() != 0)
			{
				$q4edit->next();
				$record = $q4edit->getCurrentValuesAsHash();
				?>
				<h2>Редактировать слово</h2>
		<form action="<?=basename($_SERVER['SCRIPT_FILENAME']);?>" method="POST">
		<input type="hidden" name="actAdmin" value="glossary">
		<input type="hidden" name="actionGlossary" value="Edit">
		<input type="hidden" name="id" value="<?=$record['id'];?>">
		<table cellspacing="2" cellpadding="0" border="0">
		<TR><TD>Слово:</TD><TD><input type="text" name="word" value="<?=$record['word'];?>"></TD></TR>
		<TR><TD>Текст:</TD><TD><textarea name="content"><?=$record['content'];?></textarea></TD></TR>
		<TR><TD>Редактирование:</TD><TD><input type="submit" value="Сохранить"></TD></TR>
		</table>
		</form><br />
		<a href="<?=basename($_SERVER['SCRIPT_FILENAME']);?>?actAdmin=glossary">Назад в Словарь</a>
				<?php
			
			}
			
		}
		if (isset($_POST['id']))
		{
			$word = addslashes($_POST['word']);
			$content = addslashes($_POST['content']);
			
					
			$SQL = "UPDATE " . $this->tableName . " SET word='$word' , content='$content' WHERE id='" . intval($_POST['id']) . "'";
			$this->core->db->executeQuery($SQL);
			
			$this->adminMain();
		}
	}
	
	function adminAdd()
	{
		if (!isset($_POST['actAdmin']))
		{
		?>
		<h2>Добавить слово</h2>
		<form action="<?=basename($_SERVER['SCRIPT_FILENAME']);?>" method="POST">
		<input type="hidden" name="actAdmin" value="glossary">
		<input type="hidden" name="actionGlossary" value="Add">
		<table cellspacing="2" cellpadding="0" border="0">
		<TR><TD>Слово:</TD><TD><input type="text" name="word" value=""></TD></TR>
		<TR><TD>Текст:</TD><TD><textarea name="content"></textarea></TD></TR>
		<TR><TD>Добавить:</TD><TD><input type="submit" value="Сохранить"></TD></TR>
		</table>
		</form><br />
		<a href="<?=basename($_SERVER['SCRIPT_FILENAME']);?>?actAdmin=glossary">Назад в словарь</a>
		<?php
		}
		else
		{
			if (isset($_POST['word']))
			{
				
				$word = addslashes($_POST['word']);
				$content = addslashes($_POST['content']);
				
						
				$SQL = "INSERT INTO " . $this->tableName . " ( word , content ) VALUES ( '$word' , '$content' )";
				$this->core->db->executeQuery($SQL);
				
				$this->adminMain();
			}
			else
			{
				echo ("Ошибка.");
			}
			
		}
	}
}
